package com.szsh.bigdata.open.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisPool {
    private static final Logger log = LoggerFactory.getLogger(RedisPool.class);

    private volatile static JedisPool pool;

    public static void init(String host, int port, String password, int size) {
        if(pool == null) {
            synchronized (RedisPool.class) {
                if (pool == null) {
                    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
                    jedisPoolConfig.setMaxIdle(10);
                    jedisPoolConfig.setMaxTotal(20);
                    jedisPoolConfig.setMinIdle(5);
                    jedisPoolConfig.setMaxWaitMillis(10 * 1000);
                    jedisPoolConfig.setTestWhileIdle(true);
                    jedisPoolConfig.setTestOnBorrow(false);
                    jedisPoolConfig.setTestOnReturn(false);
                    pool = new JedisPool(jedisPoolConfig, host, port, 10 * 1000, password);
                }
            }
        }
    }

    /**
     * 为指定的 k 设置值及过期时间。如果 k 已经存在，setex 命令会替换旧值和过期时间
     *
     * @param k key
     * @param seconds 过期时间
     * @param v value
     * @return return
     */
    public static String setex(String k, int seconds, String v) {
        try (Jedis jedis = pool.getResource()) {
            return jedis.setex(k, seconds, v);
        } catch (Exception e) {
            String errInfo = String.format("[RedisPool]setex failed, k=%s, v=%s, seconds=%d, err=%s", k, v, seconds, e.getMessage());
            log.error(errInfo);
            throw e;
        }
    }

    /**
     * 用于获取指定 k 的值。如果 k 不存在，返回 null
     *
     * @param k key
     * @return return
     */
    public static String get(String k) {
        try (Jedis jedis = pool.getResource()) {
            return jedis.get(k);
        } catch (Exception e) {
            String errInfo = String.format("[RedisPool]get failed, k=%s, err=%s", k, e.getMessage());
            log.error(errInfo);
            throw e;
        }
    }

    /**
     * 设置 key 的过期时间
     *
     * @param k key
     * @param seconds 过期时间
     * @return return
     */
    public static Long expire(String k, Integer seconds){
        try (Jedis jedis = pool.getResource()) {
            return jedis.expire(k, seconds);
        } catch (Exception e) {
            String errInfo = String.format("[RedisPool]expire failed, k=%s, seconds=%d, err=%s", k, seconds, e.getMessage());
            log.error(errInfo);
            throw e;
        }
    }

    /**
     * 返回 key 的剩余过期时间，单位是秒
     *
     * @param k key
     * @return return
     */
    public static Long ttl(String k){
        try (Jedis jedis = pool.getResource()) {
            return jedis.ttl(k);
        } catch (Exception e) {
            String errInfo = String.format("[RedisPool]ttl failed, k=%s, err=%s", k, e.getMessage());
            log.error(errInfo);
            throw e;
        }
    }

}